//
//  ModelFirstTopicViewController.h
//  艾听歌
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SpecialTopicViewController.h"
#import "ModuleFirstTopicDetailViewController.h"


#import "TopicFirst_FirstTableViewCell.h"


#import "ModuleFirst_TopicHomePageModel.h"


#define ViewWidth  self.view.frame.size.width
#define ViewHeight self.view.frame.size.height

#define TabelViewW  self.topicTableView.frame.size.width
#define TabelViewH  self.topicTableView.frame.size.height

static NSString *const topicTableViewCell = @"topicTableViewCell";

@interface SpecialTopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *topicTableView;

@property (nonatomic, strong) NSMutableArray *allTopicArray;

//@property (nonatomic, strong) NSMutableArray *myTopicArray;


@property (nonatomic, assign) BOOL isUpLoading;//标记上拉操作还是下拉操作,yes是上拉

@property (nonatomic, assign)NSInteger nextpage;//记录下一页


@end

@implementation SpecialTopicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    

    // 专题
    
    // 系统默认全屏显示  也就是导航栏和tabBar被延伸到了屏幕的周围 所以设置成None;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.topicTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight - 66 - 49 - 44) style:UITableViewStylePlain];
//    self.topicTableView.backgroundColor = [UIColor redColor];
//    self.topicTableView.backgroundColor = [UIColor whiteColor];
    self.topicTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    [self.view addSubview:self.topicTableView];
    
    //消除分割线
    self.topicTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //成为代理人
    self.topicTableView.delegate = self;
    self.topicTableView.dataSource = self;

    //tableview注册
    [self.topicTableView registerClass:[TopicFirst_FirstTableViewCell class] forCellReuseIdentifier:topicTableViewCell];
    
    
    //数组初始化
    self.allTopicArray = [NSMutableArray array];
//    self.myTopicArray = [NSMutableArray array];
    
    self.isUpLoading = NO;
    //2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];



}


#pragma  mark --下拉刷新
- (void)addHeader{
    
    __block SpecialTopicViewController *root = self;
    
    self.topicTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //进入刷新状态就会回调这个block
        root.nextpage = 1;//记录页码的
        root.isUpLoading = NO;//标记为下拉操作
        [root getAFNetworkingData:root.nextpage];//请求数据
        
  
        
    }];
    
    
   
#pragma mark --自动刷新(--进入程序就下拉刷新)
    [self.topicTableView.mj_header beginRefreshing];

}



#pragma mark -- 上拉加载更多
- (void)addFooter{
    
    __block SpecialTopicViewController *root = self;
    self.topicTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //进入刷新状态就会回调这个Block
        
        root.isUpLoading = YES;//标记为上拉操作
        root.nextpage++;
        [root getAFNetworkingData:root.nextpage];

        
    }];
  
}
//网络请求数据
- (void)getAFNetworkingData:(NSInteger)nextCursor{
    
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    //创建一个管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.securityPolicy setAllowInvalidCertificates:YES];
    [manager.securityPolicy setValidatesDomainName:NO];
    //识别各种类型的网址
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
//    NSLog(@"%ld", nextCursor);
    NSString *string = [NSString stringWithFormat:@"https://chanyouji.com/api/articles.json?page=%ld",nextCursor] ;
    [manager GET:string parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
//        NSLog(@"%@", responseObject);
        
        
        //******
        [netWorkManager stopMonitoring];
        
        if (self.isUpLoading == NO) {
            
            //说明是下拉,就要清空数组中的数据
            [self.allTopicArray removeAllObjects];
        }
        
        
        for (NSDictionary *dic in responseObject) {
            ModuleFirst_TopicHomePageModel *MF_THPModel = [[ModuleFirst_TopicHomePageModel alloc]init];
            [MF_THPModel setValuesForKeysWithDictionary:dic];
            
            [self.allTopicArray addObject:MF_THPModel];
            
            
        }
        if (nextCursor == 1) {
            
            [self.allTopicArray removeObjectAtIndex:0];
            
        }
        
        
        [self.topicTableView.mj_footer endRefreshing];
        [self.topicTableView.mj_header endRefreshing];
        
        [self.topicTableView reloadData];
        

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"error====%@",error);

        
    }];
}

//必须做的
//显示每个cell的方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    TopicFirst_FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:topicTableViewCell forIndexPath:indexPath];
    
    cell.contentView.layer.cornerRadius = 6;
    cell.contentView.layer.masksToBounds = YES;

    
    cell.TBCellModel = [self.allTopicArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //专题列表的轻拍手势方法实现
    __block SpecialTopicViewController *root = self;
    cell.toTopicDetail = ^(ModuleFirst_TopicHomePageModel *cellModel){
//        NSLog(@"%@", cellModel);
        
        ModuleFirstTopicDetailViewController *MFTDVC = [[ModuleFirstTopicDetailViewController alloc]init];
        UINavigationController *MFTDNaVC = [[UINavigationController alloc]initWithRootViewController:MFTDVC];
        MFTDVC.cellModel = cellModel;
        
        [root presentViewController:MFTDNaVC animated:YES completion:^{
            
            
        }];
        
        
    };
    

    return cell;
}

//每个分区中cell的个数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    NSLog(@"%@", self.allTopicArray);
    
    return self.self.allTopicArray.count;
}


//******选做的******
//每个cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return TabelViewH / 2.5;
    
}


@end
